<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/30
 * Time: 16:23
 */

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\ApiController;
use App\LogicService\Contracts\MedicalComplicationAuxiliaryLogicContract;
use App\LogicService\Contracts\MedicalComplicationLogicContract;
use App\LogicService\Contracts\MedicalComplicationPromptLogicContract;
use App\LogicService\Contracts\MedicalRecordLogicContract;
use Illuminate\Http\Request;

class ComplicationController extends ApiController
{
    protected $request;

    protected $complication;

    protected $complicationAuxiliary;

    protected $complicationPrompt;

    protected $medicalRecord;

    /**
     * ComplicationController constructor.
     * @param Request $request
     * @param MedicalComplicationLogicContract $complication
     * @param MedicalComplicationAuxiliaryLogicContract $complicationAuxiliary
     * @param MedicalComplicationPromptLogicContract $complicationPrompt
     * @param MedicalRecordLogicContract $medicalRecord
     */
    public function __construct(Request $request, MedicalComplicationLogicContract $complication,
                                MedicalComplicationAuxiliaryLogicContract $complicationAuxiliary,
                                MedicalComplicationPromptLogicContract $complicationPrompt,
                                MedicalRecordLogicContract $medicalRecord)
    {
        parent::__construct();
        $this->request = $request;
        $this->complication = $complication;
        $this->complicationAuxiliary = $complicationAuxiliary;
        $this->complicationPrompt = $complicationPrompt;
        $this->medicalRecord = $medicalRecord;

    }


    public function store()
    {

        $this->validate($this->request, [
            'record_id' => 'required|integer',
            'prompt' => 'string',
            'auxiliary_data' => 'array',
            'complication_data' => 'array',
        ]);

        $auxiliary_data = $this->request->input('auxiliary_data');
        $complication_data = $this->request->input('complication_data');
        $prompt = $this->request->input('prompt');
        $record_id = $this->request->input('record_id');

        $result = array();

        $complication_auxiliary_result = array();
        foreach ($auxiliary_data as $key => $value) {
            $complicationAuxiliary = $this->complicationAuxiliary->update($record_id, $value);
            $complication_auxiliary_result[] = $complicationAuxiliary;
        }

        $complication_result = array();
        foreach ($complication_data as $key => $value) {
            $complication = $this->complication->update($record_id, $value);
            $complication_result[] = $complication;
        }

        $result['complication'] = $complication_result;
        $result['auxiliary'] = $complication_auxiliary_result;
        $result['prompt'] = $this->complicationPrompt->update($record_id, $prompt);

        return $this->response($result);
    }


    public function getList()
    {

        $this->validate($this->request, [
            'record_id' => 'required|integer',
        ]);


        $record_id = $this->request->input('record_id');
        $result = array();

        $complication_result = $this->complication->getList($record_id);
        $complication_auxiliary_result = $this->complicationAuxiliary->getList($record_id);
        $result['complication'] = $complication_result;
        $result['auxiliary'] = $complication_auxiliary_result;
        $result['prompt'] = $this->complicationPrompt->show($record_id);
        return $this->response($result);
    }

    public function getLastList()
    {

        $this->validate($this->request, [
            'record_id' => 'integer',
            'type' => 'required|integer',
            'patient_id' => 'integer'
        ]);

        $record_id = $this->request->input('record_id');
        $type = $this->request->input('type');
        $patient_id = $this->request->input('patient_id');
        $result = array();

        $last_record = $this->medicalRecord->getLastRecord($record_id, $type, $patient_id);

        $complication_result = $this->complication->getList($last_record->id);
        $complication_auxiliary_result = $this->complicationAuxiliary->getList($last_record->id);
        $result['complication'] = $complication_result;
        $result['auxiliary'] = $complication_auxiliary_result;
        $result['prompt'] = $this->complicationPrompt->show($last_record->id);
        return $this->response($result);
    }

    public function isSave()
    {
        $this->validate($this->request, [
            'record_id' => 'required|integer'
        ]);

        $record_id = $this->request->input('record_id');

        $complication_result = $this->complication->getList($record_id);
        $complication_auxiliary_result = $this->complicationAuxiliary->getList($record_id);
        $prompt_result = $this->complicationPrompt->show($record_id);

        $is_save = 0;

        if ($complication_result || $complication_auxiliary_result || $prompt_result->prompt) {
            $is_save = 1;
        }

        $result['is_save'] = $is_save;

        return $this->response($result);
    }

}